{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b9398bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b44dde4a",
   "metadata": {},
   "source": [
    "### 读取Excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "29a4ab2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(\"本月份上衣销量数据.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c8af22a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>类型</th>\n",
       "      <th>销量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-07-01</td>\n",
       "      <td>上衣</td>\n",
       "      <td>691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-07-02</td>\n",
       "      <td>上衣</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-07-03</td>\n",
       "      <td>上衣</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期  类型   销量\n",
       "0 2021-07-01  上衣  691\n",
       "1 2021-07-02  上衣  121\n",
       "2 2021-07-03  上衣  118"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82ace268",
   "metadata": {},
   "source": [
    "### 增加移动列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e8f6aa7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"下一日\"] = df[\"销量\"].shift()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "db813acf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>类型</th>\n",
       "      <th>销量</th>\n",
       "      <th>下一日</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-07-01</td>\n",
       "      <td>上衣</td>\n",
       "      <td>691</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-07-02</td>\n",
       "      <td>上衣</td>\n",
       "      <td>121</td>\n",
       "      <td>691.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-07-03</td>\n",
       "      <td>上衣</td>\n",
       "      <td>118</td>\n",
       "      <td>121.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-07-04</td>\n",
       "      <td>上衣</td>\n",
       "      <td>879</td>\n",
       "      <td>118.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-07-05</td>\n",
       "      <td>上衣</td>\n",
       "      <td>470</td>\n",
       "      <td>879.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期  类型   销量    下一日\n",
       "0 2021-07-01  上衣  691    NaN\n",
       "1 2021-07-02  上衣  121  691.0\n",
       "2 2021-07-03  上衣  118  121.0\n",
       "3 2021-07-04  上衣  879  118.0\n",
       "4 2021-07-05  上衣  470  879.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "611dd9b5",
   "metadata": {},
   "source": [
    "### 计算日环比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "475f58c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"日同比\"] = (df[\"销量\"] - df[\"下一日\"])*100 / df[\"下一日\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "75a32af7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>类型</th>\n",
       "      <th>销量</th>\n",
       "      <th>下一日</th>\n",
       "      <th>日同比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-07-01</td>\n",
       "      <td>上衣</td>\n",
       "      <td>691</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-07-02</td>\n",
       "      <td>上衣</td>\n",
       "      <td>121</td>\n",
       "      <td>691.0</td>\n",
       "      <td>-82.489146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-07-03</td>\n",
       "      <td>上衣</td>\n",
       "      <td>118</td>\n",
       "      <td>121.0</td>\n",
       "      <td>-2.479339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-07-04</td>\n",
       "      <td>上衣</td>\n",
       "      <td>879</td>\n",
       "      <td>118.0</td>\n",
       "      <td>644.915254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-07-05</td>\n",
       "      <td>上衣</td>\n",
       "      <td>470</td>\n",
       "      <td>879.0</td>\n",
       "      <td>-46.530148</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期  类型   销量    下一日         日同比\n",
       "0 2021-07-01  上衣  691    NaN         NaN\n",
       "1 2021-07-02  上衣  121  691.0  -82.489146\n",
       "2 2021-07-03  上衣  118  121.0   -2.479339\n",
       "3 2021-07-04  上衣  879  118.0  644.915254\n",
       "4 2021-07-05  上衣  470  879.0  -46.530148"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9135013",
   "metadata": {},
   "source": [
    "### 清理临时字段，输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c3f43ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.drop(columns=[\"下一日\"], inplace=True)\n",
    "df.fillna(0.0, inplace=True)\n",
    "df[\"日同比\"] = df[\"日同比\"].map(lambda x : round(x, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "82b4757a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>类型</th>\n",
       "      <th>销量</th>\n",
       "      <th>日同比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-07-01</td>\n",
       "      <td>上衣</td>\n",
       "      <td>691</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-07-02</td>\n",
       "      <td>上衣</td>\n",
       "      <td>121</td>\n",
       "      <td>-82.49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-07-03</td>\n",
       "      <td>上衣</td>\n",
       "      <td>118</td>\n",
       "      <td>-2.48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期  类型   销量    日同比\n",
       "0 2021-07-01  上衣  691   0.00\n",
       "1 2021-07-02  上衣  121 -82.49\n",
       "2 2021-07-03  上衣  118  -2.48"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "42ef783f",
   "metadata": {},
   "outputs": [],
   "source": [
    "file = '结果文件.xlsx'\n",
    "with pd.ExcelWriter(file, datetime_format='YYYY-MM-DD') as writer:\n",
    "    df.to_excel(writer, sheet_name='日环比数据', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecf54f7d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
